home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dl / dl_ldnfilep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  5.4 KB  |  198 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /*
  25. ** dl_ldnfilep - Load any file, into private shm segments.
  26. */
  27.  
  28. #define _auxtemp _auxtemp2    /* A bug in ldfcn.h */
  29.  
  30. #ifndef mips
  31. #define  mips  (1)
  32. #endif
  33. #include <stdio.h>
  34. #include <filehdr.h>
  35. #include <nlist.h>
  36. #include <sym.h>
  37. #include <syms.h>  /* conflict with a macro n_name in nlist */
  38. #include <ar.h>
  39. #include <scnhdr.h>
  40. #include <ldfcn.h>
  41. #include <sys/types.h>
  42. #include <sys/ipc.h>
  43. #include <sys/shm.h>
  44.  
  45. #include "dl.h"
  46.  
  47. static void *allocseg(unsigned int addr, unsigned int len, int *id);
  48. static setsegro(void *tseg, int tsegid);
  49.  
  50.  
  51. #ifdef DEBUG
  52. #define D(x) (x)
  53. #else
  54. #define D(x)
  55. #endif /* DEBUG */
  56.  
  57. extern void bzero (void *, size_t);
  58.  
  59. int
  60. dl_ldnfilep(struct ldfile *ldptr)
  61. {
  62.     int i, nsect;
  63.     int tsize = 0, dsize = 0;
  64.     unsigned taddr = 0xffffffff, daddr = 0xffffffff;
  65.     void *tseg, *dseg;
  66.     struct scnhdr shdr;
  67.     int tsegid;
  68.  
  69.     /*
  70.     ** Make a first pass over the sections, and compute total text
  71.     ** and data segment sizes.
  72.     */
  73.     nsect = HEADER(ldptr).f_nscns;
  74.     D(printf("loadfile: %d sections\n", nsect));
  75.     for ( i=1; i<nsect+1; i++ ) {
  76.     if ( ldshread(ldptr, i, &shdr) == FAILURE ) {
  77.         dl_error("Cannot read section header %d", (char *)i);
  78.         return 0;
  79.     }
  80.     D(printf("loadfile: section %d=%s, 0x%x:0x%x@0x%x type 0x%x\n", i,
  81.            shdr.s_name, shdr.s_vaddr, shdr.s_size, shdr.s_scnptr, shdr.s_flags));
  82.     /*
  83.     ** We would like to use the lower 4 bits in s_flags here to decide
  84.     ** what to do with the segment (allocate/load), but they don't seem
  85.     ** to be set correctly. So, we have to fiddle.
  86.     */
  87.     switch(shdr.s_flags) {
  88.     case STYP_TEXT:
  89.         if ( tsize ) {
  90.         dl_error("Multiple text segments?", 0);
  91.         return 0;
  92.         }
  93.         tsize += shdr.s_size;
  94.         if ( taddr > shdr.s_vaddr ) taddr = shdr.s_vaddr;
  95.         break;
  96.     case STYP_DATA:
  97.     case STYP_BSS:
  98.     case STYP_RDATA:
  99.     case STYP_SDATA:
  100.     case STYP_SBSS:
  101.     case STYP_LIT8:
  102.     case STYP_LIT4:
  103.         dsize += shdr.s_size;
  104.         if ( daddr > shdr.s_vaddr ) daddr = shdr.s_vaddr;
  105.         break;
  106.     case _STYP_RESOURCE:
  107.         break;                   /* Ignore this one */
  108.     default:
  109.         dl_error("Unknown section type 0x%x", (char *)shdr.s_flags);
  110.         return 0;
  111.     }
  112.     }
  113.     /*
  114.     ** We now know the sizes. Allocate the segments.
  115.     */
  116.     D(printf("Textaddr: 0x%x, dataaddr: 0x%x\n", taddr, daddr));
  117.     D(printf("Textsize: 0x%x, datasize: 0x%x\n", tsize, dsize));
  118.     if ( (tseg = allocseg(taddr, tsize, &tsegid)) == 0 )
  119.       return 0;
  120.     if ( dsize ) {
  121.     if ( (dseg = allocseg(daddr, dsize, (int *)0)) == 0 )
  122.       return 0;
  123.     }
  124.     /*
  125.     ** Next, we make another pass over the sections and load the data
  126.     ** (or zero the section, for bss sections).
  127.     */
  128.     for(i=1; i<nsect+1; i++) {
  129.     if ( ldshread(ldptr, i, &shdr) == FAILURE) {
  130.         dl_error("Cannot read section header %d", (char *)i);
  131.         return 0;
  132.     }
  133.     if ( shdr.s_scnptr ) {
  134.         D(printf ( "Read sect %d\n", i));
  135.         if ( ldsseek(ldptr, i) == FAILURE ) {
  136.         dl_error("Cannot seek to data for section %d", (char *)i);
  137.             return 0;
  138.         }
  139.         D(printf ("At address %x\n", ftell(IOPTR(ldptr))));
  140.         if ( FREAD((void *)shdr.s_vaddr, 1, shdr.s_size, ldptr) != shdr.s_size ) {
  141.         dl_error("Cannot read data for section %d", (char *)i);
  142.             return 0;
  143.         }
  144.     } else {
  145.         bzero((char *)shdr.s_vaddr, shdr.s_size);
  146.         D(printf("Skip sect %d\n", i));
  147.     }
  148.     }
  149.     /*
  150.     ** Step 5 - Make text segment read/only.
  151.     */
  152.     if ( setsegro(tseg, tsegid) == 0 ) {
  153.         return 0;
  154.     }
  155.     return 1;
  156. }
  157.  
  158. static void *
  159. allocseg(unsigned int addr, unsigned int len, int *id)
  160. {
  161.     int shmid;
  162.     void *rv;
  163.  
  164.     if ( !dl_setrange(addr, addr+len) )
  165.       return 0;
  166.     shmid = shmget(IPC_PRIVATE, len, 0777);
  167.     D(printf("shmget=%x\n", shmid));
  168.     if ( shmid < 0 ) {
  169.     dl_error(0, "shmget");
  170.         return 0;
  171.     }
  172.     rv = shmat(shmid, (void *)addr, SHM_RND);
  173.     if ((int)rv == -1) {
  174.     dl_error(0, "shmat");
  175.         return 0;
  176.     }
  177.     if ( id ) {
  178.     *id = shmid;
  179.     } else {
  180.     shmctl(shmid, IPC_RMID);
  181.     }
  182.     D(printf("shmat=%x\n", rv));
  183.     return rv;
  184. }
  185.  
  186. static int
  187. setsegro(void *tseg, int tsegid)
  188. {
  189.     shmdt(tseg);
  190.     D(printf("setsegro ( %x, %d)\n", tseg, tsegid));
  191.     if( shmat(tsegid, tseg, SHM_RDONLY|SHM_RND) != tseg ) {
  192.     dl_error(0, "shmat(read-only text segment)");
  193.         return 0;
  194.     }
  195.     shmctl(tsegid, IPC_RMID);
  196.     return 1;
  197. }
  198.